﻿/***************************************************************
* File    : InQueue.cs
* Author  : Kyle Burgess
*
* Description:
*   Module used while building the system. Testing purposes only
*
* History :
***************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace RLGDashboard
{
    public class InQueue
    {
        public Byte CurrentQueueCount;
        private int MAX_QUEUE_SIZE = 100;
        private BluetoothTransmission[] QueueBody;
        private int firstElement;
        private int lastElement;
        private bool writeLock;
        private bool readLock;

        public InQueue()
        {
            CurrentQueueCount = 0;
            firstElement = 0;
            lastElement = 0;
            writeLock = false;
            readLock = false;
            QueueBody = new BluetoothTransmission[100];
        }

        public void writeToQueue(BluetoothTransmission readIn)
        {
            //If another thread is trying to read from the queue, wait for it to finish before we affect the queue
            while (readLock == true) ;

            //Prevent another thread from reading the queue.
            writeLock = true;

            //Write to queue
            QueueBody[lastElement] = readIn;

            //Increment the index to the next available 
            lastElement = lastElement + 1;

            //Check to see if we are wrapping around our queue.
            if (lastElement == MAX_QUEUE_SIZE)
            {
                lastElement = 0;
            }

            if (lastElement == firstElement)
            {
                Thread warningThread = new Thread(new ThreadStart(displayWarningMessage));
                warningThread.Start();

                firstElement = 0;
                lastElement = 0;
                QueueBody = new BluetoothTransmission[100];
            }

            //Allow another thread to read from queue.
            writeLock = false;

            //Increment queue size
            CurrentQueueCount++;
        }

        private void displayWarningMessage()
        {

            MessageBox.Show("Error: Queue is overflowing", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void emptyQueue()
        {
            writeLock = true;
            //Empty out the queue
            while (CurrentQueueCount > 0)
            {
                readFromQueue();
            }
            writeLock = false;
        }

        public BluetoothTransmission readFromQueue()
        {
            while (writeLock == true) ;

            readLock = true;
            BluetoothTransmission readOut = QueueBody[firstElement];

            firstElement++;

            if (firstElement == MAX_QUEUE_SIZE)
            {
                firstElement = 0;
            }

            //Decrement Queue size
            CurrentQueueCount--;

            readLock = false;
            return readOut;
        }
    }
}
